<?php
/**
 * User: i@fedax.cn
 * Time: 2018/9/6 18:50
 */

namespace app\api\model;


use think\Model;
use Yansongda\Pay\Pay;

class AliPay extends Model
{
    protected $config = [
        'app_id' => '2018020902166151',
        'notify_url' => 'https://api.quniuyuyin.com/Api/Public/notify',
        'return_url' => 'https://api.quniuyuyin.com/Api/Public/alipay_return',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp/ptDlGsTP40Ve4xWXqNZHRQEuLuE+9lz6q3uAn6mhy70HdatT4/lhCiGD3CGvb9VARNBDFmpbIIMHrmhhFKOtfxWEBei5AXdn34l6p18ZgmNWW7AytbBc/1tnkog6ueD2ysQ1+E8sAqtQ3bNiO+BaAz0ML1y5G2ngOhSBpUFk20bn3MKaZcBn5rRvPdCCsYYJOSUJYff/tLqjj8XSS/vxmjoGoB+HP5BGLSqCL2wPkp6OAdt8lzM1QUq2Oe+1SbIlh0te7HPr/XVCl/qwHEoD0Y6yeXIPTHHc3grX2Y49TxkRXNa/3yaHvp58t7wAcOpLQDAeeNPFC3Hh8VleYsIQIDAQAB',
        // 加密方式： **RSA2**
        'private_key' => 'MIIEpgIBAAKCAQEAsEp6PUQMOwWyCuzjs1b3EywDuJZRE6kgpAqI4+ybFrKPL87eB09jX0AdT5fz22d5IiWyHhxbR6YPWwB44fqlwMmUI1gzA/FvLT458UyT3SImSmq2AHFdGQld12rz7La1jwepR44ZXkhp9so8oMaBnHwWcn2BJfg8aWw2jxMGpZzURzD7qx6roNUpH0kTGwmuJetbRK9k4MNKGaBKCHYtEShwYztyr1F5j9t1YvuJXksKHDVU1u1pENK6QChL1sZl1hhn+GCemCcKdy67wdE1SKN4b3MCRl0b8yffBFkKPB/HOc3+90LYI1lHwnFTZ8nBfsn/wRw5HBzGQPnllYFDWwIDAQABAoIBAQChvmNzDJjwGiSlU30CkRq1k1+Cnx+aG0VgXG4GOJJ+e6tV4MXjDRqOTMPXwAGz3rbL9qv6Zmf1T82s370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LmVKtkKj3nnLgg8TFrXQCPSiWdAwCIdazkx5xU7bkkaUxSQcVxtrAw2JQOBHv9Im',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'debug',
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'mode' => 'optional', // 设置此参数为dev，将进入沙箱模式
    ];

    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'total_amount' => '1',
            'subject' => 'test subject - 测试',
        ];

        $alipay = Pay::alipay($this->config)->web($order);

        return $alipay;// laravel 框架中请直接 `return $alipay`
    }

    /**
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
     */
    public function aliPayReturn()
    {
        $data = Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！

        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    /**
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function notify()
    {
        $alipay = Pay::alipay($this->config);

        try {
            $data = $alipay->verify(); // 是的，验签就这么简单！

            // 请自行对 trade_status 进行判断及其它逻辑进行判断，在支付宝的业务通知中，只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号；
            // 2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额）；
            // 3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）；
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况
            \think\facade\Log::error($data->all());
        } catch (\Exception $e) {
            $e->getMessage();
        }

        return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }
}